/**  
* @project_name mfq_comm
* @Title: ImageUtil.java
* @Package com.yjd.comm.util
* @Description: TODO
* @author renbangjie renbangjie@126.com  
* @date 2014-9-1 下午1:53:36
* @version V1.0  
* <b>Copyright (c)</b> 2014医信金融信息服务（上海）有限公司-版权所有<br/>
*/
package com.yjd.comm.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * @ClassName: ImageUtil
 * @Description: TODO
 * @author renbangjie renbangjie@126.com
 * @date 2014-9-1 下午1:53:36
 */
public class ImageUtil {
	
	/**
	* @Title: process
	* @Description: 
	* @param is 图片文件流
	* @param newWidth 新的宽度
	* @param newHeight 新的高度
	* @return BufferedImage
	 * @throws IOException 
	 */
	public static BufferedImage process(InputStream is, int newWidth, int newHeight) throws IOException{
		BufferedImage image = getBufferedImage(is);
		return process(image, newWidth, newHeight);
	}
	
	/**
	* @Title: process
	* @Description: 
	* @param byte[] 图片文件
	* @param newWidth 新的宽度
	* @param newHeight 新的高度
	* @return BufferedImage
	 * @throws IOException 
	 */
	public static BufferedImage process(byte[] bytes, int newWidth, int newHeight) throws IOException{
		BufferedImage image = getBufferedImage(bytes);
		return process(image, newWidth, newHeight);
	}
	
	
	/**
	* @Title: getInputStream
	* @Description: 图片字节数组
	* @param image
	* @return InputStream
	 * @throws IOException 
	 */
	public static byte[] getBytes(BufferedImage image) throws IOException{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ImageIO.write(image, "PNG", bos);
		return bos.toByteArray();
	}
	
	
	/**
	* @Title: getInputStream
	* @Description: 图片转流
	* @param image
	* @throws IOException
	* @return InputStream
	 */
	public static InputStream getInputStream(BufferedImage image) throws IOException{
		return new ByteArrayInputStream(getBytes(image));
	}
	
	
	/**
	* @Title: process
	* @Description: 缩放图片
	* @param bytes 图片
	* @param newWidth 新的宽度
	* @param newHeight 新的高度
	* @return BufferedImage 
	 */
	public static BufferedImage process(BufferedImage image, int newWidth, int newHeight){
		//图片的原始大小
		int width = image.getWidth(null), height = image.getHeight(null); 
		//创建缩放的图片
		Image resizedImage = null;  
		if (width > height) {  
            resizedImage = image.getScaledInstance(newWidth, (newWidth * height)  
                    / width, Image.SCALE_SMOOTH);  
        } else {  
            resizedImage = image.getScaledInstance((newWidth * width) / height,  
                    newWidth, Image.SCALE_SMOOTH);  
        } 
		Image temp = new ImageIcon(resizedImage).getImage();
		BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),  
                temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
		
        Graphics g = bufferedImage.createGraphics();  
  
        g.setColor(Color.white);  
        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));  
        g.drawImage(temp, 0, 0, null);  
        g.dispose();  
  
        float softenFactor = 0.05f;  
        float[] softenArray = { 0, softenFactor, 0, softenFactor,  
                1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };  
        Kernel kernel = new Kernel(3, 3, softenArray);  
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);  
		
		return cOp.filter(bufferedImage, null); 
	}
	
	
	public static BufferedImage getBufferedImage(byte[] bytes) throws IOException{
		return getBufferedImage(new ByteArrayInputStream(bytes));
	}
	
	
	public static BufferedImage getBufferedImage(InputStream is) throws IOException{
		return ImageIO.read(is);
	}
	
}
